package com.yf.domain.security;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Objects;
import java.util.Set;

/**
 * @author chentianwei
 * @date 2023/4/17 13:45
 * @description Menu domain
 */

@Getter
@Setter
@TableName("sys_menu")
public class Menu  implements Serializable {

    @TableId(value = "menu_id", type = IdType.AUTO)
    private Long id;

    @TableField(exist = false)
    private Set<Role> roles;

    @TableField(value = "name")
    private String name;

    @TableField(value = "path")
    private String path;

    @TableField(value = "menu_sort")
    private Integer menuSort;

    @TableField(value = "type")
    private Integer type;

    @TableField(value = "title")
    private String title;

    @TableField(value = "cache")
    private Boolean cache;

    @TableField(value = "hidden")
    private Boolean hidden;

    @TableField(value = "remark")
    private String remark;

    @TableField(value = "pid")
    private Long pid;

    @TableField(value = "name")
    @Schema(name = "菜单组件名称")
    private String componentName;

    @TableField(value = "permission")
    private String permission;

    @Schema(name = "菜单图标")
    @TableField(value = "icon")
    private String icon;

    @Schema(name = "子节点数量", hidden = true)
    private Integer subCount = 0;

    @Schema(name = "外链菜单")
    @TableField(value = "i_frame")
    @JsonProperty(value = "iFrame", required = true)
    private Boolean iFrame;

    @Schema(name = "组件路径")
    private String component;

    @TableField(value = "create_time",insertStrategy = FieldStrategy.DEFAULT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    private Timestamp createTime;

    @TableField(value = "update_time",updateStrategy = FieldStrategy.ALWAYS)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    private Timestamp updateTime;

    @TableField(value = "create_by",insertStrategy = FieldStrategy.DEFAULT)
    private String createBy;

    @TableField(value = "update_by",updateStrategy = FieldStrategy.ALWAYS)
    private String updateBy;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Menu menu = (Menu) o;
        return Objects.equals(id, menu.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    public Boolean getHasChildren() {
        return subCount > 0;
    }

    public Boolean getLeaf() {
        return subCount <= 0;
    }

    public String getLabel() {
        return title;
    }
}
